package me.joshua.tools4j.service.security;

import me.joshua.tools4j.model.User;
import me.joshua.tools4j.model.security.Demo4jUserDetail;

import org.springframework.data.domain.AuditorAware;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

@Component
public class AuditorService implements AuditorAware<User> {

	@Override
	public User getCurrentAuditor() {
		Authentication authentication = SecurityContextHolder.getContext()
				.getAuthentication();
		if (authentication == null || !authentication.isAuthenticated()) {
			return null;
		}
		return ((Demo4jUserDetail) authentication.getPrincipal()).getUser();
	}
}
